package com.quan.whizenoa.data.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.quan.commons.core.MyBaseServiceImpl;
import com.quan.hr.entity.HrPost;
import com.quan.hr.service.HrPostService;
import com.quan.whizenoa.data.commons.vo.IeasySysPositionVo;
import com.quan.whizenoa.data.entity.IeasySysPosition;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

import com.quan.whizenoa.data.mapper.IeasySysPositionMapper;
import com.quan.whizenoa.data.service.IeasySysPositionService;


@Slf4j
@Service
@DS("ieasy")
public class IeasySysPositionServiceImpl extends MyBaseServiceImpl<IeasySysPositionMapper, IeasySysPosition> implements IeasySysPositionService {

    @Autowired
    private IeasySysPositionMapper mapper;

    @Autowired
    private HrPostService postService;



    /**
     * 数据读取导入
     * 数据库：
     * 岗位->职务
     * [ieasy].ieasy_sys_position -> [whizen_pmg_admin].pmg_hr_post
     * @return
     */
    @Override
    public List<IeasySysPositionVo> importIeasyPost2PmgPost() {

        // 删除新库所有数据
        this.postService.removeByIds(this.postService.list());

        final List<IeasySysPosition> list = super.list();
        // 获取一级分类
        final List<IeasySysPositionVo> collect = list.stream()
                .filter(category -> category.getPid() == null)
                .map(item -> {
                    IeasySysPositionVo bean = new IeasySysPositionVo();
                    BeanUtils.copyProperties(item, bean);

                    log.info("职务：{}, {}", item.getId(), item.getName());

                    HrPost post = new HrPost();
                    post.setName(item.getName());
                    post.setSort(item.getSort());
                    this.postService.save(post);

                    // 获取子分类
                    bean.setChildren(getChildrens(bean, list, post.getId()));

                    return bean;
                }).collect(Collectors.toList());

        return collect;
    }

    //递归查找所有菜单的子菜单
    private List<IeasySysPosition> getChildrens(IeasySysPositionVo root, List<IeasySysPosition> all, Long rootId) {

        List<IeasySysPosition> children = all.stream().filter(categoryEntity -> {
            return null != categoryEntity.getPid() && categoryEntity.getPid().equals(root.getId());
        }).map(item -> {
            IeasySysPositionVo bean = new IeasySysPositionVo();
            BeanUtils.copyProperties(item, bean);

            log.info("职务：{}, {}", item.getId(), item.getName());

            HrPost post = new HrPost();
            post.setPid(rootId);
            post.setName(item.getName());
            post.setSort(item.getSort());
            this.postService.save(post);

            //1、找到子菜单(递归)
            bean.setChildren(getChildrens(bean, all, post.getId()));

            return bean;
        }).collect(Collectors.toList());

        return children;
    }
}